﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Data.SqlClient;
using System.Configuration;
using ERPStudio;
using System.Threading;
using System.Globalization;
using System.Reflection;
using System.Data;


namespace ExBug
{
    class dscModule : ModuleClass
    {

        public dscModule(Int16 codUnit, String numeUnit,String theme)
            : base(codUnit, numeUnit,theme)
        {

        }
        /// <summary>
        /// returneaza ID-ul intern pentru autorizare - 
        /// deriva din numele modului si este o cheie de control
        /// </summary>
        /// <returns></returns>
        override public String GetModuleID()
        {
            return "05";
        }
       
        
        /// <summary>
        /// incarca meniul acestui modul
        /// </summary>
        /// <param name="myMenu"></param>
        public void LoadMenu(ToolStripMenuItem mn, Form vForm, Guid appID, Guid userID)
        {
            base.LoadMenu(mn, (FrmMainMdi)vForm, appID,userID); 
            InitStoredProcedure("Init_ExBug");
        }

        public void Parametrizare(object sender, EventArgs e)
        {
            ExBugSetupFrm frm = new ExBugSetupFrm(this);
            frm.MdiParent = mdiForm;
            frm.Show();

        }
        public void Buget(object sender, EventArgs e)
        {
            BugetFrm frm = new BugetFrm(this);
            frm.MdiParent = mdiForm;
            frm.Show();

        }

        public void Unitati_functionale(object sender, EventArgs e)
        {
            FrmMainMdi frm = mdiForm as FrmMainMdi;
            Assembly asm = frm.GetModule("esrp");
            if (asm == null)
                return;
            String numeClasa = "ESRP.UnitatiFrm";
            Type mModule = asm.GetType(numeClasa);

            if (mModule != null)
            {
                FrmRezumat newFrm = asm.CreateInstance(numeClasa, true, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null,
                   new object[] { this }, Thread.CurrentThread.CurrentCulture, new object[] { }) as FrmRezumat;

                newFrm.MdiParent = frm;
                newFrm.Show();
            }            
        }

        
        static void Despre(object sender, EventArgs e)
        {
            AboutBox frm = new AboutBox();
            frm.ShowDialog();
        }
        public void Ordonantari(object sender, EventArgs e)
        {
            OrdonantariFrm frm = new OrdonantariFrm(this);
            frm.MdiParent = mdiForm;
            frm.Show();
        }
        public void PAC(object sender, EventArgs e)
        {
            PACFrm frm = new PACFrm(this);
            frm.MdiParent = mdiForm;
            frm.Show();
        }

        public void AB(object sender, EventArgs e)
        {
            ABFrm frm = new ABFrm(this);
            frm.MdiParent = mdiForm;
            frm.Show();
        }
        public void AL(object sender, EventArgs e)
        {
            ALFrm frm = new ALFrm(this);
            frm.MdiParent = mdiForm;
            frm.Show();
        }


        public void ResponsabilCFP(object sender, EventArgs e)
        {
            ResponsabilVizaCFPFrm frm = new ResponsabilVizaCFPFrm(this);
            frm.MdiParent = mdiForm;
            frm.Show();
        }

        public void ArtAl(object sender, EventArgs e)
        {
            ArticoleAliniateFrm frm = new ArticoleAliniateFrm(this);
            frm.MdiParent = mdiForm;
            frm.Show();
        }

        public void Registrul_Proiecte(object sender, EventArgs e)
        {
            OptiuniRapFrm frmo = new OptiuniRapFrm(this,0);
            frmo.MdiParent = mdiForm;
            frmo.Show();
        }

        public void Registrul_Ordonantari(object sender, EventArgs e)
        {
            OptiuniRapFrm frmo = new OptiuniRapFrm(this,1);
            frmo.MdiParent = mdiForm;
            frmo.Show();
        }



        public void Furnizori(object sender, EventArgs e)
        {
            FrmMainMdi frm = mdiForm as FrmMainMdi;
            Assembly asm = frm.GetModule("erpforms");
            if (asm == null)
                return;

            String numeClasa = "ERPForms.PartenerFrm";
            Type mModule = asm.GetType(numeClasa);



            SqlConnection con = StudioForm.DeschideConexiune(workConnectionString);
            Object retObj = StudioForm.GetSetupValue("ExBugSetup", "AfiseazaParteneriFolositi", CodUnitate, con);
            StudioForm.InchideConexiune(con);
            Boolean PartFol = true;
            if (retObj != null && retObj != DBNull.Value)
                PartFol = Convert.ToBoolean(retObj);


            if (mModule != null)
            {
                FrmCard newFrm = asm.CreateInstance(numeClasa, true, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null,
                    new object[] { this,true,PartFol }, Thread.CurrentThread.CurrentCulture, new object[] { }) as FrmCard;
                newFrm.HandleBeforeDel += new BeforeDelHandler(newFrm_HandleBeforeDel); 
                newFrm.MdiParent = frm;
                newFrm.Show();
            }

        }

        Boolean newFrm_HandleBeforeDel(object sender, EventArgs e)
        {
            FrmCard frm = sender as FrmCard;
            if (frm.listBndSourceMain.Count > 0)
            {
                DataRowView drv = frm.listBndSourceMain[0].Current as DataRowView;
                if (drv != null && drv.Row != null)
                {
                    if(MessageBox.Show("Pentru stergere partenerului este necesara stergerea conturilor bancare asociate partenerului.\nDoriti stergerea lor?","Intrebare",
                        MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.No)
                        return false;

                    SqlCommand cmd = new SqlCommand("DELETE FROM [ParteneriConturiBancare] WHERE IDPartener=" + drv.Row["IDPartener"].ToString(),
                        StudioForm.DeschideConexiune("ESRPConnectionString"));
                    cmd.ExecuteNonQuery();


                    if(MessageBox.Show("Pentru stergere partenerului este necesara stergerea ordonantarilor asociate partenerului.\nDoriti stergerea lor?","Intrebare",
                        MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.No)
                        return false;
                    cmd.CommandText = "DELETE FROM [Ordonantari] WHERE IDPartener=" + drv.Row["IDPartener"].ToString();
                    cmd.ExecuteNonQuery();
                    StudioForm.InchideConexiune(cmd.Connection);
                }
            }

            return true;
        }

        public void Jurnale(object sender, EventArgs e)
        {
            FrmMainMdi frm = mdiForm as FrmMainMdi;
            Assembly asm = frm.GetModule("ERPForms");
            if (asm == null)
                return;

            String numeClasa = "ERPForms.JurnaleFrm";
            Type mModule = asm.GetType(numeClasa);

            if (mModule != null)
            {
                FrmRezumat newFrm = asm.CreateInstance(numeClasa, true, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null,
                   new object[] { this,true }, Thread.CurrentThread.CurrentCulture, new object[] { }) as FrmRezumat;

                newFrm.MdiParent = frm;
                newFrm.Show();
            }
        }


        public void Banci(object sender, EventArgs e)
        {
            FrmMainMdi frm = mdiForm as FrmMainMdi;
            Assembly asm = frm.GetModule("erpforms");
            if (asm == null)
                return;

            String numeClasa = "ERPForms.BanciFrm";
            Type mModule = asm.GetType(numeClasa);

            if (mModule != null)
            {
                FrmRezumat newFrm = asm.CreateInstance(numeClasa, true, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null,
                   new object[] { this }, Thread.CurrentThread.CurrentCulture, new object[] { }) as FrmRezumat;
                newFrm.MdiParent = frm;
                newFrm.Show();
            }
        }

        public void Departamente(object sender, EventArgs e)
        {
            FrmMainMdi frm = mdiForm as FrmMainMdi;
            Assembly asm = frm.GetModule("erpforms");
            if (asm == null)
                return;

            String numeClasa = "ERPForms.DepartamenteFrm";
            Type mModule = asm.GetType(numeClasa);

            if (mModule != null)
            {
                FrmRezumat newFrm = asm.CreateInstance(numeClasa, true, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null,
                   new object[] { this }, Thread.CurrentThread.CurrentCulture, new object[] { }) as FrmRezumat;
                newFrm.MdiParent = frm;
                newFrm.Show();
            }
        }

        public void Executia_Cheltuielilor(object sender, EventArgs e)
        {
            CheltuieliFrm frm = new CheltuieliFrm(this);
            frm.MdiParent = mdiForm;
            frm.Show();
        }

        public void Indicatori_Sectii(object sender, EventArgs e)
        {
            OptiuniExecutieFrm frm = new OptiuniExecutieFrm(this,0);
            frm.MdiParent = mdiForm;
            frm.Show();
        }

        public void Cheltuieli_Totale_Sectii(object sender, EventArgs e)
        {
            OptiuniExecutieFrm frm = new OptiuniExecutieFrm(this, 2);
            frm.MdiParent = mdiForm;
            frm.Show();
        }

        public void Cheltuieli_Medicamente_Sectii(object sender, EventArgs e)
        {
            OptiuniExecutieFrm frm = new OptiuniExecutieFrm(this, 3);
            frm.MdiParent = mdiForm;
            frm.Show();
        }

        public void Cheltuieli_Materiale_Sanitare_Sectii(object sender, EventArgs e)
        {
            OptiuniExecutieFrm frm = new OptiuniExecutieFrm(this, 4);
            frm.MdiParent = mdiForm;
            frm.Show();
        }

        public void Cheltuieli_Totale_Defalcate_Sectii(object sender, EventArgs e)
        {
            OptiuniExecutieFrm frm = new OptiuniExecutieFrm(this, 5);
            frm.MdiParent = mdiForm;
            frm.Show();
        }


        public void Cheltuieli_Articole(object sender, EventArgs e)
        {
            OptiuniExecutieFrm frm = new OptiuniExecutieFrm(this, 1);
            frm.MdiParent = mdiForm;
            frm.Show();
        }

        public void CaseAsigurariSanatate(object sender, EventArgs e)
        {
            FrmMainMdi frm = mdiForm as FrmMainMdi;
            Assembly asm = frm.GetModule("resuma");
            if (asm == null)
                return;

            String numeClasa = "ResUma.CaseAsigSanatateFrm";
            Type mModule = asm.GetType(numeClasa);

            if (mModule != null)
            {
                FrmRezumat newFrm = asm.CreateInstance(numeClasa, true, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null,
                   new object[] { this }, Thread.CurrentThread.CurrentCulture, new object[] { }) as FrmRezumat;
                newFrm.MdiParent = frm;
                newFrm.Show();
            }
        }


    }
}
